from rest_framework import serializers
from .models import Match, Team


class MatchListSerializer(serializers.ModelSerializer):
    category_name = serializers.CharField(source='category.name', read_only=True)

    class Meta:
        model = Match
        fields = ('title', 'introduction', 'pub_date', 'views', 'category_name')


class MatchDetailSerializer(serializers.ModelSerializer):
    author = serializers.ReadOnlyField(source='author.nickname')

    class Meta:
        model = Match
        fields = '__all__'


class TeamListSerializer(serializers.ModelSerializer):
    leader_name = serializers.ReadOnlyField(source='leader.nickname')
    match_name = serializers.ReadOnlyField(source='match.name')

    class Meta:
        model = Team
        fields = ('members', 'match', 'match_name', 'leader', 'leader_name', 'name')
        read_only_fields = ('leader',)
